package org.pp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.pp.mapper.UserMapper;
import org.pp.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.pp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    IUserService userService;

    @GetMapping("/testQueryWrapper")
    public String testQueryWrapper() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "小明update");
        User user = userMapper.selectOne(queryWrapper);

        QueryWrapper<User> queryWrapperList = new QueryWrapper<>();
        queryWrapperList.eq("name", "小明");
        List<User> users = userMapper.selectList(queryWrapperList);

        log.info(user.toString());
        users.forEach(e -> log.info(e.toString()));

        userMapper.selectList(queryWrapperList, new ResultHandler<User>() {
            @Override
            public void handleResult(ResultContext<? extends User> resultContext) {
                log.info(resultContext.getResultObject().toString());
            }
        });

        QueryWrapper<User> queryWrapperListLike = new QueryWrapper<>();
        queryWrapperList.like("name", "小明");
        userMapper.selectList(queryWrapperListLike);

        return "oK,test";
    }

    @GetMapping("/testLambdaQueryWrapper")
    public String testLambdaQueryWrapper() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //SFunction <User,String> function= User::getName;
        //SFunction <User,String> function= e->{ return e.getName();};
        //SFunction <User,String> function= e->e.getName();
        //SFunction<User,String> function= User::getName;

        lambdaQueryWrapper.eq(User::getName, "小明update");
        User user = userMapper.selectOne(lambdaQueryWrapper);

        LambdaQueryWrapper<User> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.like(User::getName, "小明");
        List<User> users = userMapper.selectList(likeLambdaQueryWrapper);
        //users.forEach(e-> System.out.println(e));
        users.forEach(System.out::println);

        return "OK, test";
    }


    @GetMapping("/testLambdaUpdateWrapper")
    public String testLambdaUpdateWrapper() {
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<User>();
        lambdaUpdateWrapper.eq(User::getName, "小明");
        //lambdaUpdateWrapper.set(User::getPwd, "654321");

        lambdaUpdateWrapper.setSql("pwd='123456'");
        userMapper.update(lambdaUpdateWrapper);
        return "OK,test";
    }

    @GetMapping("/testUpdateWrapper")
    public String testUpdateWrapper() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
        updateWrapper.eq("name", "小明");
        //lambdaUpdateWrapper.set(User::getPwd,"654321");

        updateWrapper.setSql("pwd='123456'");
        userMapper.update(updateWrapper);

        return "Ok,test";
    }

    @GetMapping("/testUpdate")
    public String testUpdate() {
        User u = new User();
        u.setPwd("123456");
        u.setName("小东");
        userMapper.update(u,
                new LambdaQueryWrapper<User>().eq(User::getName, "小明"));

        return "OK, test";
    }

    @GetMapping("/testUserService")
    public String testUserService() {
        User user = userService.getById(2);
        System.out.println(user.toString());
        System.out.println(userService.countAll());
        System.out.println(userService.count());

        return "OK,test";
    }


    @GetMapping("/testPage")
    public IPage<User> testPage(@RequestParam(name = "current",defaultValue = "1", required = false) long current,
                                @RequestParam(name = "size",defaultValue = "10", required = false) long size) {
        Page<User> pg = new Page<>(current, size);
        IPage<User> res = userMapper.getPage(pg, "小东");
        log.info(String.format("%s %s", res.getRecords().size(), res.getTotal()));

        Page<User> page = userService.page(pg, new LambdaQueryWrapper<User>().eq(User::getName, "小东"));
        Page<User> page2 = userMapper.selectPage(pg, new LambdaQueryWrapper<User>().eq(User::getName, "小东"));

        return page;
    }


    @GetMapping("/testPageHelper")
    public PageInfo<User> testPageHelper(@RequestParam(name = "current",defaultValue = "1", required = false) int current,
                                @RequestParam(name = "size",defaultValue = "10", required = false) int size) {

        PageHelper.startPage(current,size);
        List<User> list = userService.list();
        return new PageInfo<User>(list);
    }


    @GetMapping("deleteLogic")
    private String deleteLogic() {
        userService.removeById(2);
        return "OK";
    }


    @GetMapping("getById")
    private User getById() {
        return userService.getById(2);
    }




}
